var ticket_reply = new Array();

function ticket_unset_button( bid )
{
	document.getElementById(bid).style.opacity = 0.4;
	document.getElementById(bid).style.filter = "alpha(opacity=60)";
	document.getElementById(bid).style.fontWeight = "normal";
	document.getElementById(bid).style.color = "#000000";
	document.getElementById(bid).style.cursor = "default";
	document.getElementById(bid).onclick = "javascript:return false;";
	document.getElementById(bid).disabled = true;
}

function ticket_status_checker( status )
{
	if ( status == 0 )
	{
		ticket_unset_button("action_0");	
	}
	else if ( status == 1 )
	{
		ticket_unset_button("action_1");
	}
	else if ( status == 2 )
	{
		ticket_unset_button("action_2");
	}
	else if ( status == 3 )
	{
		ticket_unset_button("action_3");
	}
	
	if ( permission("ticket_edit",true) != true )
	{
		ticket_unset_button("action_0");
		ticket_unset_button("action_1");
		ticket_unset_button("action_2");
		ticket_unset_button("action_3");
	}
}

function ticket_set_status( ticket_id, ticket_status )
{
	window.location.href = site_root_domain + '/?site=ticket&act=edit_do&id='+ticket_id+'&set_status='+ticket_status;	
}

function ticket_edit_reply( reply_id )
{
	ticket_close_open_editor();
	
	// Read object
	var reply = document.getElementById("reply_editor_" + reply_id);
	
	// Backup
	ticket_reply[reply_id] = reply.innerHTML;
	
	// Create WYSIWYG
	reply.innerHTML = "";
	document.getElementById("reply_form_"+reply_id).style.display = (is_ie == 0 ? "table" : "block");
	eval("tinyMCE.editors.reply_content_"+reply_id+".setContent(ticket_reply[reply_id])");
}

function ticket_close_open_editor()
{
	//document.getElementById("ticket_reply_form").innerHTML = "none";	
}

function ticket_reply_edit_submit( edit_form, reply_id )
{
	document.getElementById("reply_content_"+reply_id).value = tinyMCE.get("reply_content_"+reply_id).getContent();
	
	var status = AjaxRequest.submit(
		edit_form,{
			'onLoading':function(req){ document.getElementById("reply_editor_"+reply_id).innerHTML = site_loading; },
			'onSuccess':function(req){ ticket_reply_result(reply_id, req.responseText) }
		}
	);

	return false;
}

function ticket_reply_result(reply_id, text)
{
	document.getElementById("reply_editor_"+reply_id).innerHTML = text;
	document.getElementById("reply_form_"+reply_id).style.display = "none";
}

function ticket_reply_cancel(reply_id)
{
	document.getElementById("reply_editor_"+reply_id).innerHTML = ticket_reply[reply_id];
	document.getElementById("reply_form_"+reply_id).style.display = "none";
}

function ticket_pick_checker(ticket_id)
{
	if ( ticket_pick_user == "0" )
	{
		document.getElementById("pick_up").style.display = "inline-block";
		document.getElementById("pick_down").style.display = "none";
		
		return true;
	}
	else if ( ticket_pick_user == ticket_user )
	{
		document.getElementById("pick_up").style.display = "none";
		document.getElementById("pick_down").style.display = "inline-block";
		document.getElementById("ticket_reply_form").style.display = (is_ie == 0 ? "table" : "block");
		
		return true;
	}
	else
	{
		document.getElementById("pick_up").style.display = "inline-block";
		document.getElementById("pick_text").innerHTML = lang_pick_error;
	}
	
	return false;
}

function ticket_pick_up(ticket_id)
{
	AjaxRequest.get({
		'url':site_root_domain+'/?site=ticket&act=show&is_pick=1&pick_status=1&id='+ticket_id
		,'onLoading':function(req){ document.getElementById("pick_text").innerHTML = "<img align='absmiddle' src='"+site_img_url+"/icon_loading_small.gif'>"; }
		,'onSuccess':function(req){
			
			var picker = req.responseText.split("|");
			
			if ( picker[0] == 1 )
			{
				document.getElementById("pick_up").style.display = "none";
				document.getElementById("pick_down").style.display = "inline-block";
				document.getElementById("ticket_reply_form").style.display = (is_ie == 0 ? "table" : "block");
				document.getElementById("pick_text").innerHTML = "";
			}
			else
			{
				document.getElementById("pick_up").style.display = "inline-block";
				document.getElementById("pick_text").innerHTML = lang_pick_error;
			}
						
			ticket_pick_user = picker[1];
			document.getElementById("ticket_pick_user").innerHTML = picker[2];
			document.getElementById("ticket_pick_time").innerHTML = picker[3];
		}
	});
}

function ticket_pick_down(ticket_id)
{
	AjaxRequest.get({
		'url':site_root_domain+'/?site=ticket&act=show&is_pick=1&pick_status=0&id='+ticket_id
		,'onLoading':function(req){ document.getElementById("pick_text").innerHTML = "<img align='absmiddle' src='"+site_img_url+"/icon_loading_small.gif'>"; }
		,'onSuccess':function(req){
			
			var picker = req.responseText.split("|");
			
			if ( picker[0] == 1 )
			{
				document.getElementById("pick_up").style.display = "inline-block";
				document.getElementById("pick_down").style.display = "none";
				document.getElementById("ticket_reply_form").style.display = "none";
				document.getElementById("pick_text").innerHTML = "";
			}
			else
			{
				document.getElementById("pick_up").style.display = "inline-block";
				document.getElementById("pick_text").innerHTML = lang_pick_error;	
			}
				
			ticket_pick_user = picker[1];
			document.getElementById("ticket_pick_user").innerHTML = picker[2];
			document.getElementById("ticket_pick_time").innerHTML = picker[3];
		}
	});
}

var ticket_pick_id = 0;

function ticket_pick_update()
{
	if ( ticket_pick_user == ticket_user )
	{
		AjaxRequest.get({
			'url':site_root_domain+'/?site=ticket&act=show&is_pick=1&pick_status=2&id='+ticket_pick_id
			,'onLoading':function(req){ document.getElementById("pick_text").innerHTML = "<img align='absmiddle' src='"+site_img_url+"/icon_loading_small.gif'>"; }
			,'onSuccess':function(req){
				
				var picker = req.responseText.split("|");
			
				if ( picker[0] == 1 )
				{
					var picker = req.responseText.split("|");

					document.getElementById("pick_text").innerHTML = "";
				}
				
				ticket_pick_user = picker[1];
				document.getElementById("ticket_pick_user").innerHTML = picker[2];
				document.getElementById("ticket_pick_time").innerHTML = picker[3];
			}
		});
	}
	
	setTimeout("ticket_pick_update()",ticket_pick_timeout*1000);
}

function ticket_pick_cron(ticket_id)
{
	ticket_pick_id = ticket_id;
	ticket_pick_update();
}



